﻿/*
*┌────────────────────────────────────────────────┐
*│　描    述：DeviceBindBusiness                                                   
*│　作    者： (Jxw)                                             
*│　版    本：1.0     
*│　功能描述：                                          
*│　创建时间：2021/6/27 19:24:18                        
*└────────────────────────────────────────────────┘
*/

using Data;
using Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utility;

namespace Business
{
    public class DeviceBindBusiness
    {
        private static DeviceBindBusiness m_data = null;


        private DeviceBindBusiness()
        {

        }

        public static DeviceBindBusiness DataBusiness
        {
            get
            {
                if (m_data == null)
                    m_data = new DeviceBindBusiness();
                return m_data;
            }
        }


        #region 时钟部分
        /// <summary>
        /// 每十分钟获取一次设备信息
        /// </summary>
        public void TimerStarttell()
        {
            System.Timers.Timer InitTimer = new System.Timers.Timer();
            //InitTimer.Interval = 3600 * 1000;//每一个小时
            InitTimer.Interval = AppConfig.TimingTime;//每十分钟
            InitTimer.Enabled = true;
            InitTimer.Elapsed += InitTimer_Elapsed;
        }

        /// <summary>
        /// 指定时间内时钟的执行事件-获取设备信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void InitTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            GetDeviceBindInfo();
        }

        #endregion

        public void GetDeviceBindInfo()
        {
            try
            {
                Hashtable ht = new Hashtable();
                List<TPerson> personList = MainDapperData.GetPersonInfo();
                List<DeviceBindInfo> deviceList = ToldDapperData.GetDeviceInfo();
                deviceList.ForEach(q =>
                {
                    var plateNum = personList.FirstOrDefault(x => x.workNum == q.workNum)?.plateNum;
                    q.plateNum = plateNum;
                });


                ht.Add("creationTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                ht.Add("msgId", Guid.NewGuid().ToString());
                ht.Add("vendorId", "ANKE");
                ht.Add("deviceBindInfo", deviceList);
                string json = JsonHelper.SerializeObject(ht);
                EventUtility.OnShowMsg("开始推送设备绑定信息", false);

                string ptResult = HttpService.PostResponse(AppConfig.DDJJUrl, json, AppConfig.appCode, AppConfig.sign);
                RetDTO ret = JsonHelper.DeserializeObject<RetDTO>(ptResult);
                if (ret.ack.code == "0")
                {
                    EventUtility.OnShowMsg("推送设备绑定信息成功！", false);
                    //LogUtility.Debug("WDPushBusiness/pushPatientResuorceData", "推送调度急救数据（120专线）成功" + ret);
                }
                else
                {
                    EventUtility.OnShowMsg("推送设备绑定信息失败！", true);
                    LogUtility.Debug("DeviceBindBusiness/GetDeviceBindInfo", "状态码：" + ret.ack.code + ",   " + ret.ack.text);
                }

            }
            catch (Exception ex)
            {
                LogUtility.Error("DeviceBindBusiness/GetDeviceBindInfo", ex.ToString());
            }

        }


        public object GetDeviceBindingRelation(string deviceCode, string deviceType)
        {
            Hashtable ht = new Hashtable();
            try
            {
                List<DeviceBindInfo> deviceList = new List<DeviceBindInfo>();
                List <TPerson> personList = MainDapperData.GetPersonInfo();
                deviceList = ToldDapperData.GetDeviceInfo();
                if (string.IsNullOrEmpty(deviceCode))
                {
                    deviceList.ForEach(q =>
                    {
                        var plateNum = personList.FirstOrDefault(x => x.workNum == q.workNum)?.plateNum;
                        q.plateNum = plateNum;
                    });

                    switch (deviceType)
                    {
                        //case "0":
                        //    deviceList.Where(x => x.deviceType == "极致").ToList();
                        //    break;
                        case "1":
                            deviceList=deviceList.Where(x => x.deviceType == "极致").ToList();
                            break;
                        case "2":
                            deviceList=deviceList.Where(x => x.deviceType == "卓尔").ToList();
                            break;
                        default:
                            break;
                    }
                }
                else
                {
                    //string workNum = deviceList.FirstOrDefault(x => x.deviceCode == deviceCode)?.workNum;
                    //string plateNum = personList.FirstOrDefault(x => x.workNum == workNum)?.plateNum;

                    deviceList.ForEach(q =>
                    {
                        var plateNum = personList.FirstOrDefault(x => x.workNum == q.workNum)?.plateNum;
                        q.plateNum = plateNum;
                    });
                    deviceList = deviceList.Where(x => x.deviceCode == deviceCode).ToList();
                }

                ht.Add("creationTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                ht.Add("msgId", Guid.NewGuid().ToString());
                ht.Add("vendorId", "ANKE");
                ht.Add("deviceBindInfo", deviceList);
            }
            catch (Exception ex)
            {
                EventUtility.OnShowMsg("查询设备绑定信息失败", true);
                LogUtility.Error("DeviceBindBusiness/GetDeviceBindInfo", ex.ToString());
            }
            return ht;
        }

    }
}
